/**
 * Created by Administrator on 2016/1/26.
 */

require.config(require_config);
define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'dialog'
], function ($, common, ready, template, Collection, Model, dialog) {
    var id = getUrlPath(3);
    var index = function () {
        this.init();
    }

    index.prototype = {
        init: function () {

            this.fetch();
            this.event();
            var app_type = $('input[name=app_type]:checked').val();

            if (app_type == 'apk') {
                $('.apk-item').show();
                $("input[name='app_type'][value='apk']").attr("checked", "checked");
                $('#versionName').show();
                $('#versionCode').show();
            } else {
                $('.apk-item').hide();
                $("input[name='app_type'][value='apk']").attr("checked", "checked");
                $('#versionName').show();
                $('#versionCode').show();
            }

        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'upgrade',
                model: {
                    url: _global.url.api + 'upgrade',
                    event: {
                        '#btn-publish:click': 'publish',
                    },
                    publish: function (e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定发布为正式版本吗?',
                            ok: function () {
                                if (model.put()) {
                                    _this.collection.fetch();
                                }
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    }
                },
                callback: function () {
                    loaded();
                },
            });
            _this.collection.fetch();
        },

        event: function () {
            var _this = this;
            $('#btn-save').on('click', function (e) {
                e.preventDefault();
                var model = new Model({
                    url: _global.url.api + 'upgrade',
                    data: {
                        versionName: $('input[name="versionName"]').val(),
                        versionCode: $('input[name="versionCode"]').val(),
                        changeLog: $('textarea[name="changeLog"]').val(),
                        url: $('input[name="url"]').val(),
                        app_type: $('input[name="app_type"]:checked').val(),
                        validFrom: $('input[name="validFrom"]').val(),
                        device: $('textarea[name="device"]').val(),
                    },
                    valid_config: {
                        versionName: 'required',
                        versionCode: 'required',
                        changeLog: 'required',
                        //url: 'required',
                        app_type: 'required',
                        device: 'required',
                    },
                    success: function () {
                    }
                });
                model.post();
                _this.collection.reload();
            });

            $('#full').on('click', function (e) {
                e.preventDefault();
                var url = $('input[name="url"]').val();
                var urlArr = url.split('haintc_v');
                var number = urlArr[(urlArr.length - 1)].split('.');

                var versionName = urlArr[(urlArr.length - 1)].replace(/.apk/, "").replace(/.plist/, "");
                var versionCode = '';

                for (var i = 0; i < 3; i++) {
                    if (i == '0') {
                        versionCode += number[i];
                    } else if (i > 0 && i < 3) {
                        versionCode += number[i].length < 2 ? ('0' + number[i]) : number[i];
                    }
                }

                $('input[name="versionCode"]').val(versionCode);
                $('input[name="versionName"]').val(versionName);

            });

            $('.app_type').on('change', function () {
                var type = $(this).val();
                if (type == 'apk') {
                    $('.apk-item').show();
                    $('#versionName').attr('disabled', true);
                    $('#versionCode').attr('disabled', true);
                } else if (type == 'ios') {
                    $('.apk-item').hide();
                    $('#versionName').removeAttr('disabled');
                    $('#versionCode').removeAttr('disabled');
                }

            })
        },
        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        },
    }

    new index();
});
